package com.example.easyscript.bean;

import com.example.easyscript.bean.task.StepBean;
import com.example.easyscript.bean.task.TaskBean;
import com.example.easyscript.dto.BaseMatchResult;
import com.example.easyscript.service.SystemImageServive;
import com.example.easyscript.utils.ImageUtils;
import com.example.easyscript.utils.JnaUtils;
import com.example.easyscript.utils.RobotUtils;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.awt.*;

/**
 * 步骤处理器
 */
public abstract class StepHandler {

    @Resource
    private SystemImageServive systemImageServive;

    /**
     * 执行这个步骤
     *
     * @param stepBean 步骤
     */
    public boolean executionStep(StepBean stepBean, TaskBean taskBean) {
        String backgroundPath = systemImageServive.getTaskCurrentCapture(taskBean);
        String stepButtonPath = stepBean.getPictureUrl();
        System.out.println("正在运行步骤 "+stepBean.getStepName()+" 关联图片是 "+stepBean.getImageName()+ "图片路径："+stepBean.getPictureUrl());
        Rectangle rectangle = JnaUtils.getRect(taskBean.getWindowsName());
        BaseMatchResult templateCenterPoint = ImageUtils.getTemplateCenterPoint(stepButtonPath, backgroundPath);
        Point base = new Point(rectangle.x + templateCenterPoint.getLeftUpPoint().x, rectangle.y + templateCenterPoint.getLeftUpPoint().y);
        org.opencv.core.Point clickPoint = new org.opencv.core.Point(base.x, base.y);
        Point robotPoint = new Point((int) (clickPoint.x + templateCenterPoint.getCol() / 2), (int) (clickPoint.y + templateCenterPoint.getRow() / 2));
        RobotUtils.clickPoint(robotPoint);
        return true;
    }


    /**
     * 判断对应步骤类型是否可以处理
     */
    public abstract boolean canSolve(StepBean stepBean);

    public abstract boolean preStep(StepBean stepBean);


}
